package spark.stream

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
 * @className Stream
 * @author sjw
 * @date 2020/11/19 15:08
 * @description: TODO
 */
object Stream {
  def main(args: Array[String]): Unit = {

    fileStreaming()

  }

  def initSparkStreaming(appName:String,url:String): StreamingContext ={
    val sparkConf=new SparkConf().setAppName(appName).setMaster(url)
    new StreamingContext(sparkConf,Seconds(10))
  }

  def fileStreaming(): Unit ={
    val streamingContext=initSparkStreaming("fileStreaming","local[2]")
    val lines=streamingContext.textFileStream("G:\\SparkData\\Streaming")
    val words=lines.flatMap(_.split(" "))
    val wordCount=words.map(x=>(x,1)).reduceByKey(_+_)
    wordCount.print()
    // 开始接收数据和处理流程
    streamingContext.start()
    // 等待处理结束
    streamingContext.awaitTermination()
  }

}
